package com.cardone.platform.common.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dto.EntityTreeDto;
import com.cardone.platform.common.service.EntityTreeService;

/**
 * 实体树
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/common/entityTree/")
@Controller
public class EntityTreeController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 实体树：删除
	 *
	 * @param deleteEntityTree
	 *          实体树对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final EntityTreeDto deleteEntityTree) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityTreeService.class).deleteByIds(deleteEntityTree);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityTreeController.log);
				}
			}
		};
	}

	/**
	 * 实体树：分页查询
	 *
	 * @param findEntityTree
	 *          实体树对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final EntityTreeDto findEntityTree, final Model model) {
		final EntityTreeDto entityTree = ContextHolder.getBean(EntityTreeService.class).findById(EntityTreeDto.class, findEntityTree);

		model.addAttribute("entityTree", entityTree);
	}

	/**
	 * 实体树：编辑
	 *
	 * @param findEntityTree
	 *          实体树对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<EntityTreeDto>> findByIdJson(final EntityTreeDto findEntityTree, final Model model) {
		return new Callable<ReturnDataDto<EntityTreeDto>>() {
			@Override
			public ReturnDataDto<EntityTreeDto> call() throws Exception {
				try {
					final EntityTreeDto entityTree = ContextHolder.getBean(EntityTreeService.class).findById(EntityTreeDto.class, findEntityTree);

					return ReturnDataUtils.newReturnData(entityTree);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityTreeController.log);
				}
			}
		};
	}

	/**
	 * 实体树：查询
	 *
	 * @param findListEntityTree
	 *          实体树对象
	 * @return 实体树对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<EntityTreeDto>>> findListByLikeCodeJson(final EntityTreeDto findListEntityTree) {
		return new Callable<ReturnDataDto<List<EntityTreeDto>>>() {
			@Override
			public ReturnDataDto<List<EntityTreeDto>> call() throws Exception {
				try {
					final List<EntityTreeDto> entityTreeList = ContextHolder.getBean(EntityTreeService.class).findListByLikeCode(EntityTreeDto.class, findListEntityTree);

					return ReturnDataUtils.newReturnData(entityTreeList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityTreeController.log);
				}
			}
		};
	}

	/**
	 * 实体树：分页查询
	 *
	 * @param paginationEntityTree
	 *          实体树对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<EntityTreeDto>>> paginationByLikeCodeJson(final EntityTreeDto paginationEntityTree) {
		return new Callable<ReturnDataDto<PaginationDto<EntityTreeDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<EntityTreeDto>> call() throws Exception {
				try {
					final PaginationDto<EntityTreeDto> entityTreePagination = ContextHolder.getBean(EntityTreeService.class).paginationByLikeCode(EntityTreeDto.class, paginationEntityTree);

					return ReturnDataUtils.newReturnData(entityTreePagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityTreeController.log);
				}
			}
		};
	}

	/**
	 * 实体树：分页查询
	 *
	 * @param paginationEntityTree
	 *          实体树对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final EntityTreeDto paginationEntityTree, final Model model) {
		final PaginationDto<EntityTreeDto> entityTreePagination = ContextHolder.getBean(EntityTreeService.class).paginationByLikeCode(EntityTreeDto.class, paginationEntityTree);

		model.addAttribute("entityTreePagination", entityTreePagination);
	}

	/**
	 * 实体树：保存
	 *
	 * @param saveEntityTree
	 *          实体树对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final EntityTreeDto saveEntityTree) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityTreeService.class).saveByIdOrCode(EntityTreeDto.class, saveEntityTree);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityTreeController.log);
				}
			}
		};
	}
}
